import { i18n } from '/@/i18n/index'
import request from '/@/utils/request';
import { ElMessageBox } from 'element-plus';

//验证不为空
const notNull = ({
    required: true,
    message: "必填/Required.",
})

const validateNotChinese = (rule: any, value: any, callback: any)=>{
    const patt = /^[\x00-\xff]{0,}$/;
    if(value && !patt.test(value)){
        callback(new Error())
    }else{
        callback();
    }
}
//验证不为中文，且长度50以内
const notChinese = ({
    message: "不能输入中文/No Chinese word.",
    validator: validateNotChinese,
})

//验证以英文字母开头
const validateStartWithEn = (rule: any, value: any, callback: any)=>{
    const patt = /^[a-zA-Z]\S*$/;
    if(value && !patt.test(value)){
        callback(new Error())
    }else{
        callback();
    }
}
//验证不为中文，且长度50以内
const startWithEn = ({
    message: "须以英文字母开头/Must start with [a-z,A-Z]",
    validator: validateStartWithEn,
})

//验证邮箱
const email = ({
    message: "不正确的格式/Incorrect format.",
    type: 'email',
})



const num2 = ({
    message: '只能输入数值，精确到两位小数/Only numerical values can be entered, accurate to two decimal places',
    validator: (rule: any, value: any, callback: any)=>{
        const patt = /^[0-9]{1,10}(\.[0-9]{0,2})?$/;
        if(value && !patt.test(value)){
            callback(new Error())
        }else{
            callback();
        }
    }
})

const numValidator = ({
    message: '只能输入数字，整数部分最多8位，小数部分最多2位/Only numbers can be entered, up to 8 digits before decimal point and 2 digits after',
    validator: (rule: any, value: any, callback: any) => {
        // 正则表达式解释：
        // ^[0-9]{1,8} 匹配1到8位数字（整数部分）
        // (\.[0-9]{1,2})? 可选的小数部分，包含一个小数点和1到2位数字
        const patt = /^[0-9]{1,8}(\.[0-9]{1,2})?$/;
        
        // 空值不验证（如果需要必填验证，可以另外加规则）
        if (value === undefined || value === null || value === '') {
            return callback();
        }
        
        if (!patt.test(value)) {
            callback(new Error());
        } else {
            callback();
        }
    }
});

//整数
const integer = ({
    message: '只能输入正整数/Only positive integers can be entered',
    validator: (rule: any, value: any, callback: any)=>{
        const patt = /^\d+$/;
        if(value && !patt.test(value)){
            callback(new Error())
        }else{
            callback();
        }
    }
})
//100以内整数
const integer100 = ({
    message: i18n.global.t('validate.integer'),
    validator: (rule: any, value: any, callback: any)=>{
        const patt = /^(?:0|[1-9][0-9]?|100)$/;
        if(value && !patt.test(value)){
            callback(new Error())
        }else{
            callback();
        }
    }
})

/**
 * 弱验证
 */
const weakValidate = (url: string, param: any): Promise<boolean> => {
    let promise: Promise<boolean>  = new Promise((resolve, rejct) => {
        request.post(url, param).then(res => {
            const msg = res?.data;
            if (!msg) {
                resolve(true);
            } else {
                ElMessageBox.confirm(msg, i18n.global.t('item.warning'), {
                    confirmButtonText: i18n.global.t('btn.confirm'),
                    cancelButtonText: i18n.global.t('btn.cancel'),
                    type: 'warning',
                    dangerouslyUseHTMLString: true,
                }).then(()=>{
                    resolve(true);
                }).catch(() => {
                    rejct();
                });
            }
        })
    })
    return promise;
}

const num = ({
    message: i18n.global.t('validate.num'),
    validator: (rule: any, value: any, callback: any)=>{
        const patt = /^\d+$/;
        if(value && !patt.test(value)){
            callback(new Error())
        }else{
            callback();
        }
    }
})

export default {
    notNull,
    notChinese,
    email,
    num2,
    integer,
    integer100,
    startWithEn,
    weakValidate,
    num,
    numValidator
};